const util = require('../../utils/util.js');
Page({
  /** * 页面的初始数据 */
  data: {
    isActive: null,
    listMain: [],
    fixedTitle: null,
    toView: 'inTo0',
    oHeight: [],
    scroolHeight: 0,
    fixedTop: 0,
    keyword: '',
    showClear: false
  },
  //关键字搜索
  onSearch() {
    this.getVehicleBrand()
  },
  onInput(e) {
    const value = e.detail.value;
    this.setData({
      showClear: value !== ''
    });
  },
  clearInput() {
    this.setData({
      keyword: '',
      showClear: false
    });
  },
  //前往选择车系页面
  goSelectCarSeries(e) {
    const { billid } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/myClientDetails/myClientDetails?billid=' + billid,
    })
  },
  //获取所有汽车品牌
  getVehicleBrand() {
    const that = this;
    let { keyword } = this.data;
    util.request('/itf/common/orgitem/queryMyMemberListByPage.json', {
      pageSize: -1,
      pageNo: -1,
      keyword
    }).then(function (res) {
      if (res.success) {
        let data = res.data;
        let arr = [];
        let count = 1;
        for (let key in data) {
          arr.push({
            id: count,
            region: key,
            items: data[key]
          })
          count++
        }
        that.setData({
          listMain: arr
        })
        that.getBrands();
      }
    });
  },
  //点击右侧字母导航定位触发
  scrollToViewFn: function (e) {
    var that = this;
    var _id = e.target.dataset.id;
    for (var i = 0; i < that.data.listMain.length; ++i) {
      if (that.data.listMain[i].id === _id) {
        that.setData({
          isActive: _id, toView: 'inTo' + _id, fixedTitle: that.data.listMain[i].region
        })
        break;
      }
    }
  },
  // 页面滑动时触发
  onPageScroll: function (e) {
    this.setData({ scroolHeight: e.detail.scrollTop });
    for (let i in this.data.oHeight) {
      if (e.detail.scrollTop < this.data.oHeight[i].height) {
        this.setData({
          isActive: this.data.oHeight[i].key,
          fixedTitle: this.data.oHeight[i].name
        });
        return false;
      }
    }
  },
  // 处理数据格式，及获取分组高度
  getBrands: function () {
    var that = this;
    var number = 0
    //计算分组高度,wx.createSelectotQuery()获取节点信息
    for (let i = 0; i < that.data.listMain.length; ++i) {
      wx.createSelectorQuery().select('#inTo' + that.data.listMain[i].id).boundingClientRect(function (rect) {
        number = rect.height + number; var newArry = [{ 'height': number, 'key': rect.dataset.id, "name": that.data.listMain[i].region }]
        that.setData({
          oHeight: that.data.oHeight.concat(newArry)
        })
      }).exec();
    };
  },

  onLoad: function (options) {
    var that = this;
    that.getVehicleBrand();
  }
})
